<?php

namespace Apps\AppApi\Method\Note;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Phalcon\Mvc\Model;
use Apps\Common\Models\AccountBook;


/**
 * 记一笔
 * @author      545
 * @date        2016年3月4日
 * @version     1.1.2
 * @copyright   Copyright 2015 meelier.com
 */
class AddAbNote extends BaseMethod
{
    
    private $success = 0;
    private $id = 0;

    public function run()
    {
        //验证是否登录
        if(false == $this->verifyUserAuth()){
            return false;
        }
        
    	$ab_cashnum = $this->getDataItem('ab_cashnum');
    	$ab_cardnum = $this->getDataItem('ab_cardnum');
    	$ab_innoroutid = $this->getDataItem('ab_innoroutid');
    	$child_inoroutid = $this->getDataItem('child_inoroutid');
    	$ab_type = $this->getDataItem('ab_type');
    	$ab_memberid = $this->getDataItem('ab_memberid');
    	$ab_arrears = $this->getDataItem('ab_arrears');
    	$remark = $this->getDataItem('remark');
    	$addtime = $this->getDataItem('addtime',date("Y-m-d H:i:s", time()));
    	$addtime = date("Y-m-d H:i:s",strtotime($addtime));
    	$userid = $this->getUserAuth()->userId;
    	//验证参数是否合法
    	if (empty($ab_cashnum)&&empty($ab_cardnum)){
    	    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR,'刷卡现金支付不能全为空');
    	}
    	if (in_array([1,2],$ab_type)){
    	    return $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR,"记一笔类型不正确");
    	}
    	//数据重复检查
//     	if (!empty($ab_cashnum)){
//     	    $where[] = "ab_cashnum = {$ab_cashnum}";
//     	}if (!empty($ab_cardnum)){
//     	    $where[] = "ab_cardnum = {$ab_cardnum}";
//     	}if (!empty($ab_innoroutid)){
//     	    $where[] = "ab_innoroutid = {$ab_innoroutid}";
//     	}if (!empty($ab_memberid)){
//     	    $where[] = "ab_memberid = {$ab_memberid}";
//     	}if (!empty($ab_arrears)){
//     	    $where[] = "ab_arrears = {$ab_arrears}";
//     	}if (!empty($remark)){
//     	    $where[] = "remark = '{$remark}'";
//     	}if (!empty($addtime)){
//     	    $where[] = "addtime = '{$addtime}'";
//     	}
    	
//     	$where = implode(' AND ', $where);
//     	$where .= " AND ab_type={$ab_type} AND user_id = {$userid}";
//     	$info = AccountBook::findFirst($where);
    	
//     	if ($info){//检查是否重复写入
//     	    return $this->errorLog(ResultStatus::DATABASE_ERROR,"添加的数据已存在");
//     	}
    	
    	$data['ab_cashnum'] = $ab_cashnum;
    	$data['ab_cardnum'] = $ab_cardnum;
    	$data['ab_innoroutid'] = $ab_innoroutid;
    	$data['child_inoroutid'] = $child_inoroutid;
    	$data['ab_type'] = $ab_type;
    	$data['ab_memberid'] = $ab_memberid;
    	$data['ab_arrears'] = $ab_arrears;
    	$data['remark'] = $remark;
    	$data['createtime'] = date("Y-m-d H:i:s", time());
    	$data['user_id'] = $userid;
    	$data['addtime'] = $addtime;
    	
    	$AccountBook = new AccountBook();
    	
    	if ($AccountBook->save($data)){//保存记一笔
    	    $this->id = $AccountBook->ab_id;
    	    $this->success = 1;
    	    
    	    $this->setResult([
    	        'id' => $this->id,
    	        'success' => $this->success
    	    ]);
    	    
    	}
    	else {
    	    $this->errorLog(ResultStatus::DATABASE_ERROR,"写入失败");
    	}
        
        
    }

}